/*
 * @Author: yanjikun
 * @Date: 2021-06-18 10:04:03
 * @LastEditors: yanjikun
 * @LastEditTime: 2022-05-31 09:56:10
 * @Description: file content
 */
import { App } from 'vue'
import { createWebHistory, createRouter, RouteRecordRaw } from 'vue-router'
import { whiteList } from './constant'
import { createRouterGuards } from './router-guards'
import outsideLayout from './staticModules/outsideLayout'
export const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Layout',
    component: () => import(/* webpackChunkName: "layout" */ '@/layouts/index.vue'),
    meta: {
      title: '首页',
    },
    children: [],
  },
  ...outsideLayout,
]
const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_PUBLIC_PATH),
  routes: routes as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

/**重置路由 */
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !whiteList.some((n) => n === name)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export function setupRouter(app: App) {
  // 创建路由守卫
  app.use(router)
  createRouterGuards(router)
}
export default router
